package controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import service.IBackDataService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ykj on 16/1/11.
 */
public abstract class BaseController extends Controller implements IBackgroundController{

    protected void renderData(IBackDataService service){
        String pageStr = getPara("page");
        String rowsStr = getPara("rows");
        int page = pageStr!=null?Integer.parseInt(pageStr):1;
        int rows = rowsStr!=null?Integer.parseInt(rowsStr):10;

        Page dataPage = service.findByPage(page, rows);
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        jsonMap.put("total", dataPage.getTotalRow());//total键 存放总记录数，必须的
        jsonMap.put("rows", dataPage.getList());//rows键 存放每页记录 list
        renderJson(jsonMap);
    }

    protected void renderSaveResult(int id, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (id != -1) {
            map.put("success", true);
            map.put("id", id);
        } else {
            map.put("success", false);
        }
        renderJson(map);
    }

    protected void renderDelResult(boolean result){
        Map<String, Object> map = new HashMap<String, Object>();
        if (result) {
            map.put("success", true);
        } else {
            map.put("success", false);
        }
        renderJson(map);
    }

    protected void renderColumns(List fields,List titles){
        Map<String,List> map = new HashMap<String, List>();
        map.put("fields",fields);
        map.put("titles",titles);
        renderJson(map);
    }
}
